package com.pandora.common.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.IOException;
import java.util.Map;

/**
 * @author developer
 */
public class HttpUtil {

    private static final String URL = "https://api.dpandora.cn/bcrypt";
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded;charset=utf-8";
    private static final String AUTH = "Authorization";
    private static final String AUTH_VALUE = "10-90ecab4fc00f05aab1a9d6d274bb7135";

    public static String sendPost(String type, int port, String host, String method, Map<String, String> headers, Map<String, String> params){
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(type+"://"+host+":"+port+method);

        for(Map.Entry<String, String> e : headers.entrySet()){
            postMethod.setRequestHeader(e.getKey(), e.getValue());
        }
        JSONObject jsonObject = new JSONObject();
        for(Map.Entry<String, String> p : params.entrySet()){
            jsonObject.put(p.getKey(), p.getValue());
        }
        try {
            RequestEntity re = new StringRequestEntity(jsonObject.toString(),"application/json", "UTF-8");
            postMethod.setRequestEntity(re);
            int statusCode = httpClient.executeMethod(postMethod);
            String res = new String(postMethod.getResponseBodyAsString().getBytes("UTF-8"));
            if(statusCode != MagicConst.NUM_200){
                return null;
            }
            return res;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String sendPostPassword(String password){
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(URL);

        NameValuePair[] paramPairs = new NameValuePair[1];
        NameValuePair nameValuePair = new NameValuePair();
        nameValuePair.setName("password");
        nameValuePair.setValue(password);
        paramPairs[0] = nameValuePair;
        try {
            postMethod.setRequestHeader(CONTENT_TYPE, CONTENT_TYPE_VALUE);
            postMethod.setRequestHeader(AUTH, AUTH_VALUE);
            postMethod.setRequestBody(paramPairs);
            int statusCode = httpClient.executeMethod(postMethod);
            String res = new String(postMethod.getResponseBodyAsString().getBytes("UTF-8"));
            if(statusCode != MagicConst.NUM_200){
                return null;
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            return jsonObject.get("digest")==null?null:String.valueOf(jsonObject.get("digest"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean sendPut(String pwd, String digestPwd) {
        HttpClient httpClient = new HttpClient();
        PutMethod putMethod = new PutMethod(URL);

        putMethod.setRequestHeader(CONTENT_TYPE, CONTENT_TYPE_VALUE);
        putMethod.setRequestHeader(AUTH, AUTH_VALUE);

        try {
            RequestEntity re = new StringRequestEntity("password="+pwd+"&digest="+digestPwd,"application/x-www-form-urlencoded", "UTF-8");
            putMethod.setRequestEntity(re);
            int statusCode = httpClient.executeMethod(putMethod);
            if (statusCode == MagicConst.NUM_204) {
                return true;
            }
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] argv){
        String s = sendPostPassword("123456");
        //$2b$12$qkv3XfFp7hZlhXB8bbeUFORq7FM6boXvyQJ9xQhYVxto7A2AJdcn6
        boolean b = sendPut("123456", s);
        System.out.println(s);
        System.out.println(b);
    }
}
